<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use common\models\shop\ShopGoods;
use kartik\datetime\DateTimePicker;
use common\models\shop\ShopGoodsCategory;
use yii\helpers\ArrayHelper;
use kartik\file\FileInput;
use common\models\shop\ShopDispatch;
use yii\web\JsExpression;
use kartik\tabs\TabsX;
use yii\helpers\Url;
/* @var $this yii\web\View */
/* @var $model common\models\shop\ShopGoods */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="row">
    <div class="col-lg-12">
        <section class="panel">
        <div class="panel-body">

            <div class="adv-table editable-table ">
                <div class="shop-goods-form">
                    <?php $form = ActiveForm::begin([
                        'options' => ['class' => 'form-horizontal','enctype' => 'multipart/form-data'],
                        'fieldConfig' => [
                            'template' => "<div class='col-xs-3 col-sm-2 text-right'>{label}</div><div class='col-xs-9 col-sm-7'>{input}</div><div class='col-xs-12 col-xs-offset-3 col-sm-3 col-sm-offset-0'>{error}</div>",
                        ],

                    ]); ?>

                        <div class="row">
                            <div class="col-md-5 col-md-offset-1">
                                <?= $form->field($model, 'cateid')->dropDownList(ArrayHelper::map(ShopGoodsCategory::find()->where('parentid!=0')->asArray()->all(),'id','name')) ?>
                            </div>
                            <div class="col-md-5">
                                <?= $form->field($model, 'status')->dropDownList(ShopGoods::$statues) ?>
                            </div>
                        </div>


                        <div class="row">
                            <div class="col-md-5 col-md-offset-1">
                                <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>

                            </div>
                            <div class="col-md-5">
                                <?= $form->field($model, 'displayorder')->textInput(['maxlength' => true]) ?>
                            </div>
                        </div>

                    <div class="row">
                        <div class="col-md-5 col-md-offset-1">
                            <?= $form->field($model, 'unit')->textInput(['maxlength' => true]) ?>
                        </div>
                        <div class="col-md-5">
                            <?= $form->field($model, 'viewcount')->textInput() ?>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-5 col-md-offset-1">
                            <?= $form->field($model, 'goodssn')->textInput(['readonly' => true,'value' =>$model->isNewRecord?date('YmdHi').rand(100,999):$model->goodssn,'maxlength' => true]) ?>
                        </div>

                        <div class="col-md-5">
                            <?= $form->field($model, 'weight')->textInput(['maxlength' => true])->label('重量（kg）') ?>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-5 col-md-offset-1">
                            <?= $form->field($model, 'marketprice')->textInput() ?>
                        </div>
                        <div class="col-md-5">
                            <?= $form->field($model, 'productprice')->textInput(['maxlength' => true]) ?>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-5 col-md-offset-1">
                            <?= $form->field($model, 'originalprice')->textInput(['maxlength' => true]) ?>
                        </div>
                        <div class="col-md-5">
                            <?= $form->field($model, 'costprice')->textInput() ?>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-5 col-md-offset-1">
                            <?= $form->field($model, 'sales')->textInput() ?>
                        </div>
                        <div class="col-md-5">
                            <?= $form->field($model, 'total')->textInput() ?>
                        </div>
                    </div>




                    <div class="row">
                        <div class="col-md-5 col-md-offset-1">
                            <?= $form->field($model, 'isnew')->dropDownList(ShopGoods::$isnew,['style'=>'float:left;width:300px']) ?>

                        </div>
                        <div class="col-md-5">
                            <?= $form->field($model, 'ishot')->dropDownList(ShopGoods::$ishot,['style'=>'float:left;;width:300px']) ?>

                        </div>
                    </div>


                    <div class="row">
                        <div class="col-md-5 col-md-offset-1">
                            <?= $form->field($model, 'isrecommand')->dropDownList(ShopGoods::$recommand,['style'=>'float:left;;width:300px']) ?>
                        </div>
                        <div class="col-md-5">
                            <?= $form->field($model, 'istime')->dropDownList(ShopGoods::$istime,['style'=>'float:left;;width:300px']) ?>
                        </div>
                    </div>


                    <div class="row">
                        <div class="col-md-5 col-md-offset-1">
                            <?= $form->field($model, 'timeend')->widget(DateTimePicker::classname(), [
                                'options' => ['placeholder' => '限时活动结束时间'],
                                'pluginOptions' => [
                                    'autoclose'=>true,
                                    'format' => 'yyyy-mm-dd hh:ii:ss'
                                ]
                            ]);
                            ?>
                        </div>
                        <div class="col-md-5">
                            <?= $form->field($model, 'timestart')->widget(DateTimePicker::classname(), [
                                'options' => ['placeholder' => '限时活动开始时间'],
                                'pluginOptions' => [
                                    'autoclose'=>true,
                                    'format' => 'yyyy-mm-dd hh:ii:ss'
                                ]
                            ]);
                            ?>
                        </div>
                    </div>

                    <?= $form->field($model, 'isprebuy')->dropDownList(ShopGoods::$isprebuy) ?>
                    <?= $form->field($model, 'pigid')->widget(\kartik\select2\Select2::className(), [
                        'options' => ['placeholder' => '请输入溯源仔猪的编号 ...'],
                        'initValueText'=>$model->pig?$model->pig->no:'',
                        'pluginOptions' => [
                            'allowClear' => true,
                            'minimumInputLength' => 1,//重要
                            'language' => [
                                'errorLoading' => new JsExpression("function () { return 'Waiting...'; }"),
                            ],
                            'ajax' => [
                                'url' => '/business/search/search-pig',
                                'dataType' => 'json',
                                'data' => new JsExpression('function(params) { return {q:params.term}; }'),
                                'cache' => true
                            ],
                            'width' => '400px',
                            'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                            'templateResult' => new JsExpression('function(res) { return res.text; }'),
                            'templateSelection' => new JsExpression('function (res) { return res.text; }'),
                        ],
                    ]);
                    ?>





                        <?= $form->field($model, 'thumb')->widget(FileInput::classname(), [
                            'options' => ['multiple' => false],
                            'pluginOptions' => [
                                // 需要预览的文件格式
                                'previewFileType' => 'image',
                                // 预览的文件
                                'initialPreview' => \common\components\Upload::checkGetUrl($model->thumb,false,'thumb',false),
                                // 需要展示的图片设置，比如图片的宽度等
                                'initialPreviewConfig' => \common\components\Upload::initialPreviewConfig($model,'thumb',ShopGoods::tableName(),$model->id),
                                // 是否展示预览图
                                'initialPreviewAsData' => true,
                                'showUpload' => false,
                                'overwriteInitial' => false,
                            ],
                        ]); ?>

                        <?= $form->field($model, 'mutithumb[]')->widget(FileInput::classname(), [
                            'options' => ['multiple' => true],
                            'pluginOptions' => [
                                // 需要预览的文件格式
                                'previewFileType' => 'image',
                                // 预览的文件
                                'initialPreview' => \common\components\Upload::checkGetUrl($model->mutithumb,true,'mutithumb',false),
                                // 需要展示的图片设置，比如图片的宽度等
                                'initialPreviewConfig' => \common\components\Upload::initialPreviewConfig($model,'mutithumb',ShopGoods::tableName(),$model->id),
                                // 是否展示预览图
                                'initialPreviewAsData' => true,
                                'showUpload' => false,
                                'overwriteInitial' => false,
                            ],
                        ]); ?>


                        <?= $form->field($model, 'description')->textarea() ?>
                        <?= $form->field($model,'content')->widget('kucha\ueditor\UEditor',[
                            'clientOptions' => [
                                //编辑区域大小
                                'initialFrameHeight' => '350',
                                //设置语言
                                'lang' =>'zh-cn', //中文为 zh-cn
                                //定制菜单
                                'toolbars' => [
                                    [
                                        'fullscreen', 'source', 'undo', 'redo', '|',
                                        'fontsize',
                                        'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'removeformat',
                                        'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|',
                                        'forecolor', 'backcolor', '|',
                                        'lineheight', '|',
                                        'indent', '|',
                                        'snapscreen','insertimage'
                                    ],
                                ]
                            ]
                        ]); ?>

                    <div class="form-group">
                        <?= Html::submitButton($model->isNewRecord ? '确认提交' : '确认更新', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
                    </div>
                    <?php ActiveForm::end(); ?>
                </div>
            </div>
        </div>
    </section>
</div>
</div>


<script>
    <?php $this->beginBlock('js_end') ?>
    //排序
    $('.field-shopgoods-mutithumb').on('filesorted', function(event, params) {
        var data = [];
        $.each(params.stack,function(k,v){
            data.push({'path':v.caption,'table':v.extra.table,'field':v.extra.field,'id':v.extra.id,'idname':v.extra.idname});
        })
        $.post('/site/sortfile',{'data':data},function(res){
            console.log(res)
        });
    });

    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>
</script>